<template>
	<scroll-view class="full box relative coupon-list-wrap" scroll-y>
		<view class="record-item" v-for="(item,index) in list" :key="index">
			<coupon-item :type="type" :record="item" @success="successHandle" />
		</view>
		<view class="empty-wrapper" v-if="isEmpty">
			<u-empty text="暂无数据" :icon="sourceUrl + '/data-empty.png'" />
		</view>
		<view class="loadmore-wrapper" v-if="showLoading">
			<u-loadmore icon-color="#3c9cff" :status="status" loading-icon="semicircle" />
		</view>
	</scroll-view>
</template>

<script>
	import { LOAD_MORE } from '@/constant/index.js'
	import CouponItem from './coupon-item.vue'
	export default {
		props: ['type'],
		components: { CouponItem },
		methods: {
			updateMyList() {
				if (this.type <= 0) return
				this.getMyList(true)
			},
			successHandle() {
				if (this.type !== 0) return
				this.initData(true)
			},
			async getMyList(clear = false) {
				if (clear) {
					this.page = 1
					this.status = LOAD_MORE.LOADING
					this.isEmpty = false
					this.list = []
					this.showLoading = false
				}
				if (this.status === LOAD_MORE.NO_MORE) return
				this.status = LOAD_MORE.LOADING
				const { page, pageSize } = this
				const resp = await this.$request({
					url: "/DiscountCoupon/myCouponList",
					data: { page, list_rows: pageSize },
				})
				const { code, data, msg } = resp
				if (code !== 1) {
					this.showLoading = true
					return this.$toast({ title: msg })
				}
				const { data: list, current_page, per_page, total } = data
				if (!!list && list.length) {
					this.list.push(...list)
				}
				this.isEmpty = this.list.length === 0
				const hasMore = per_page * current_page < total
				this.showLoading = true
				if (hasMore) {
					this.page += 1
					this.status = LOAD_MORE.LOAD_MORE
					return
				}
				this.status = LOAD_MORE.NO_MORE
			},
			async initData(clear = false) {
				if (clear) {
					this.page = 1
					this.status = LOAD_MORE.LOADING
					this.isEmpty = false
					this.list = []
					this.showLoading = false
				}
				if (this.status === LOAD_MORE.NO_MORE) return
				this.status = LOAD_MORE.LOADING
				const { page, pageSize } = this
				const resp = await this.$request({
					url: "/DiscountCoupon/getCouponList",
					data: { page, list_rows: pageSize },
				})
				console.log(resp, 'resp');
				const { code, data, msg } = resp
				if (code !== 1) return this.$toast({ title: msg })
				const { data: list, current_page, per_page, total } = data
				if (!!list && list.length) {
					this.list.push(...list)
				}
				this.isEmpty = this.list.length === 0
				if (!this.showLoading) {
					this.showLoading = true
				}
				const hasMore = per_page * current_page < total
				if (hasMore) {
					this.page += 1
					this.status = LOAD_MORE.LOAD_MORE
					return
				}
				this.status = LOAD_MORE.NO_MORE
			},
		},
		created() {
			uni.$on("updateMyList", this.updateMyList)
			if (this.type === 0) {
				return this.initData()
			}
			this.getMyList()
		},
		destroyed() {
			uni.$off("updateMyList", this.updateMyList)
		},
		data() {
			return {
				list: [],
				page: 1,
				pageSize: 15,
				isEmpty: false,
				status: LOAD_MORE.LOAD_MORE,
				showLoading: true,
			}
		}
	}
</script>

<style lang="scss" scoped>
	.coupon-list-wrap {
		.record-item {
			padding: 0 32rpx;
			margin-bottom: 32rpx;

			&:last-child {
				margin-bottom: 0;
			}
		}
	}
</style>